﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Nissen_Model;

namespace Nissen_Manage.Forms
{
    public partial class FormAnswerPaperSelect : Form
    {
        private readonly AnswerPaperService _answerPaperService;
        private string _subjectCourseId;
        private IList<AnswerPaper> _answerPapers;
        private readonly BindingSource _bindingSource;

        public string PageNumber { get; set; }

        public string SubjectCourseId
        {
            set { this._subjectCourseId = value; }

            get { return this._subjectCourseId; }
        }

        public FormAnswerPaperSelect()
        {
            InitializeComponent();
            _answerPaperService = new AnswerPaperService();

            _bindingSource = new BindingSource();

            //this.SubjectCourseId = "1";
        }

        private void FormAnswerPaperSelect_Load(object sender, EventArgs e)
        {
            //Image img = Image.FromFile(@"\\58.2.221.84\NissenMark\AnswerPaper\2014\20140719\数学\数学-6-@-1.jpg");
            //this.axPreview1.ShowFile(@"\\58.2.219.57\NissenMarkManager\20130518\数学\4-@-1.jpg", 1);
            //this.pictureBox1.ImageLocation = @"\\58.2.221.84\NissenMark\AnswerPaper\2014\20140719\数学\数学-6-@-1.jpg";
            //this.pictureBox1.Image = img;
            //this.pictureBox1.LoadAsync();
            pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;



            this.openFileDialog1.Filter = "JPG|*.jpg";
            this.openFileDialog1.RestoreDirectory = true;

            _answerPapers = _answerPaperService.GetAnswerPapers(_subjectCourseId,PageNumber);
            _bindingSource.DataSource = _answerPapers;
            this.dataGridView1.DataSource = _bindingSource;
        }



        private void button1_Click(object sender, EventArgs e)
        {
            string filepath = "";
            //_answerPapers.RemoveAt(1);
            if(DialogResult.OK ==
                this.openFileDialog1.ShowDialog())
            {
                filepath =
                    this.openFileDialog1.SafeFileName;

                var answerpaper = new AnswerPaper();

                answerpaper.SubjectCourseID = this._subjectCourseId;
                answerpaper.SubjectCoursePage = this.PageNumber;
                answerpaper.AnswerPaperPath = this.openFileDialog1.FileName;
                answerpaper.FileName = this.openFileDialog1.SafeFileName;
                if (_answerPapers == null)
                    _answerPapers = new List<AnswerPaper>();
                _answerPapers.Add(answerpaper);

                this._answerPaperService.AddAnswerPaper(this._answerPapers);

                this._bindingSource.ResetBindings(true);

                //this.axPreview1.ShowFile(this.openFileDialog1.FileName,0);
                pictureBox1.LoadAsync(this.openFileDialog1.FileName);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //this.axPreview1.ActualSize();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            //this.axPreview1.BestFit();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow vrow in this.dataGridView1.Rows)
            {
                if((bool)vrow.Cells["Column1"].EditedFormattedValue)
                    foreach (var item in this._answerPapers)
                    {
                        if(item.ID.Equals(vrow.Cells["ID"].Value))
                        {
                            this._answerPaperService.RemoveAnswerPaper(item);
                            _answerPapers.Remove(item);
                            break;
                        }
                           
                    }

            }

            this._bindingSource.ResetBindings(true);
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            var item =
                this.dataGridView1.Rows[e.RowIndex].DataBoundItem as AnswerPaper;

            //this.axPreview1.ShowFile(item.AnswerPaperPath, 0);
            if (item != null) pictureBox1.LoadAsync(item.AnswerPaperPath);
        }
    }
}
